// pages/editName/editName.js
import {
  request
} from '../../request/index.js'
const app = getApp()
Page({

  data: {
    username: "",
    editValue: '',
    userInfo: {},
    user: {},
    http: ''
  },
  onLoad: function () {
    let userInfo = typeof wx.getStorageSync('userInfo') === 'string' ? JSON.parse(wx.getStorageSync('userInfo')) : wx.getStorageSync('userInfo');
    let user = typeof wx.getStorageSync('user') === 'string' ? JSON.parse(wx.getStorageSync('user')) : wx.getStorageSync('user');
    let username = user.user_name;
    this.setData({
      username,
      user,
      userInfo,
      http: app.globalData.http
    })
  },
  editValue(val) {
    this.setData({
      editValue: val.detail.value
    })
  },
  finish() {
    request({
      url:'user/updUserName',
      data: {
        user_name: this.data.editValue,
        open_id: this.data.user.open_id
      }
    }).then((result) => {
      console.log(result);
      this.setData({
        user: result.data.data
      });
      wx.setStorageSync('user', result.data.data);
      wx.navigateBack({
        delta: 1,
      })
    })
  }
})